﻿using Demo.PcWebSite.Tokens;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.IdentityModel;

namespace Demo.PcWebSite.Controllers
{

    [ApiController]
    [Route("Token")]
    public class TokenController : AbpController
    {
        public IIdentityModelAuthenticationService authenticationService { get; set; }


        /// <summary>
        /// 客户端Token
        /// </summary>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<string> CreateClientToken(string clientId, string clientSecret, string serviceName)
        {
            try
            {
                // 配置权限服务信息
                IdentityClientConfiguration idc = new IdentityClientConfiguration
                {
                    Authority = "https://localhost:44393",
                    ClientId = clientId,
                    ClientSecret = clientSecret,
                    GrantType = "client_credentials", //授予客户端权限
                    Scope = serviceName + " address email phone profile roles" // 具体访问哪些微服务
                };

                // 创建token
                string token = await authenticationService.GetAccessTokenAsync(idc);

                // 存储token
                TokenSave.Token = token;

                return token;
            }
            catch (Volo.Abp.AbpException abpEx)
            {
                // 记录ABP框架特定的异常
                Console.WriteLine($"ABP Exception: {abpEx.Message}");
                throw;
            }
            catch (Exception ex)
            {
                // 记录所有其他异常
                Console.WriteLine($"Exception: {ex.Message}");
                if (ex.InnerException != null)
                {
                    Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
                }
                throw;
            }
        }
        /// <summary>
        /// 客户端用户Token
        /// </summary>
        /// <returns></returns>
        [HttpPost("UserLogin")]
        public async Task<string> CreateClientUserToken(string clientId, string clientSecret, string serviceName,string userName,string userPwd)
        {
            try
            {
                // 配置权限服务信息
                IdentityClientConfiguration idc = new IdentityClientConfiguration
                {
                    Authority = "https://localhost:44393",
                    ClientId = clientId,
                    ClientSecret = clientSecret,
                    UserName = userName,
                    UserPassword = userPwd,
                    GrantType = "password", //根据用户名+密码授权
                    Scope = serviceName + " address email phone profile roles" // 具体访问哪些微服务
                };

                // 创建token
                string token = await authenticationService.GetAccessTokenAsync(idc);

                // 存储token
                TokenSave.Token = token;

                return token;
            }
            catch (Volo.Abp.AbpException abpEx)
            {
                // 记录ABP框架特定的异常
                Console.WriteLine($"ABP Exception: {abpEx.Message}");
                throw;
            }
            catch (Exception ex)
            {
                // 记录所有其他异常
                Console.WriteLine($"Exception: {ex.Message}");
                if (ex.InnerException != null)
                {
                    Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
                }
                throw;
            }
        }

    }
}
